package com.voxel.sense.infomsg.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.voxel.sense.infomsg.BuildConfig;

import java.io.File;

public class InstallUtils {


  public synchronized static void openFile(Context context, File apkFile) {
//     = new File(fileFullName);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//            Uri contentUri = FileProvider.getUriForFile(context, "com.xxxx.xxxx.fileprovider", apkFile);
      Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", apkFile);
      intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }
    else {
      intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    if (context.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
    }

  }
}
